home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_timeout.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  6KB  |  163 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''Unit tests for socket timeout feature.'''
  5. import unittest
  6. from test import test_support
  7. skip_expected = not test_support.is_resource_enabled('network')
  8. import time
  9. import socket
  10.  
  11. class CreationTestCase(unittest.TestCase):
  12.     '''Test case for socket.gettimeout() and socket.settimeout()'''
  13.     
  14.     def setUp(self):
  15.         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  16.  
  17.     
  18.     def tearDown(self):
  19.         self.sock.close()
  20.  
  21.     
  22.     def testObjectCreation(self):
  23.         self.assertEqual(self.sock.gettimeout(), None, 'timeout not disabled by default')
  24.  
  25.     
  26.     def testFloatReturnValue(self):
  27.         self.sock.settimeout(7.3449999999999998)
  28.         self.assertEqual(self.sock.gettimeout(), 7.3449999999999998)
  29.         self.sock.settimeout(3)
  30.         self.assertEqual(self.sock.gettimeout(), 3)
  31.         self.sock.settimeout(None)
  32.         self.assertEqual(self.sock.gettimeout(), None)
  33.  
  34.     
  35.     def testReturnType(self):
  36.         self.sock.settimeout(1)
  37.         self.assertEqual(type(self.sock.gettimeout()), type(1.0))
  38.         self.sock.settimeout(3.8999999999999999)
  39.         self.assertEqual(type(self.sock.gettimeout()), type(1.0))
  40.  
  41.     
  42.     def testTypeCheck(self):
  43.         self.sock.settimeout(0)
  44.         self.sock.settimeout(0x0L)
  45.         self.sock.settimeout(0.0)
  46.         self.sock.settimeout(None)
  47.         self.assertRaises(TypeError, self.sock.settimeout, '')
  48.         self.assertRaises(TypeError, self.sock.settimeout, u'')
  49.         self.assertRaises(TypeError, self.sock.settimeout, ())
  50.         self.assertRaises(TypeError, self.sock.settimeout, [])
  51.         self.assertRaises(TypeError, self.sock.settimeout, { })
  52.         self.assertRaises(TypeError, self.sock.settimeout, (0.0+0.0j))
  53.  
  54.     
  55.     def testRangeCheck(self):
  56.         self.assertRaises(ValueError, self.sock.settimeout, -1)
  57.         self.assertRaises(ValueError, self.sock.settimeout, -0x1L)
  58.         self.assertRaises(ValueError, self.sock.settimeout, -1.0)
  59.  
  60.     
  61.     def testTimeoutThenBlocking(self):
  62.         self.sock.settimeout(10)
  63.         self.sock.setblocking(1)
  64.         self.assertEqual(self.sock.gettimeout(), None)
  65.         self.sock.setblocking(0)
  66.         self.assertEqual(self.sock.gettimeout(), 0.0)
  67.         self.sock.settimeout(10)
  68.         self.sock.setblocking(0)
  69.         self.assertEqual(self.sock.gettimeout(), 0.0)
  70.         self.sock.setblocking(1)
  71.         self.assertEqual(self.sock.gettimeout(), None)
  72.  
  73.     
  74.     def testBlockingThenTimeout(self):
  75.         self.sock.setblocking(0)
  76.         self.sock.settimeout(1)
  77.         self.assertEqual(self.sock.gettimeout(), 1)
  78.         self.sock.setblocking(1)
  79.         self.sock.settimeout(1)
  80.         self.assertEqual(self.sock.gettimeout(), 1)
  81.  
  82.  
  83.  
  84. class TimeoutTestCase(unittest.TestCase):
  85.     '''Test case for socket.socket() timeout functions'''
  86.     fuzz = 2.0
  87.     
  88.     def setUp(self):
  89.         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  90.         self.addr_remote = ('www.python.org', 80)
  91.         self.addr_local = ('127.0.0.1', 25339)
  92.  
  93.     
  94.     def tearDown(self):
  95.         self.sock.close()
  96.  
  97.     
  98.     def testConnectTimeout(self):
  99.         _timeout = 0.001
  100.         self.sock.settimeout(_timeout)
  101.         _t1 = time.time()
  102.         self.failUnlessRaises(socket.error, self.sock.connect, self.addr_remote)
  103.         _t2 = time.time()
  104.         _delta = abs(_t1 - _t2)
  105.         self.assert_(_delta < _timeout + self.fuzz, 'timeout (%g) is more than %g seconds more than expected (%g)' % (_delta, self.fuzz, _timeout))
  106.  
  107.     
  108.     def testRecvTimeout(self):
  109.         _timeout = 0.02
  110.         self.sock.connect(self.addr_remote)
  111.         self.sock.settimeout(_timeout)
  112.         _t1 = time.time()
  113.         self.failUnlessRaises(socket.error, self.sock.recv, 1024)
  114.         _t2 = time.time()
  115.         _delta = abs(_t1 - _t2)
  116.         self.assert_(_delta < _timeout + self.fuzz, 'timeout (%g) is %g seconds more than expected (%g)' % (_delta, self.fuzz, _timeout))
  117.  
  118.     
  119.     def testAcceptTimeout(self):
  120.         _timeout = 2
  121.         self.sock.settimeout(_timeout)
  122.         self.sock.bind(self.addr_local)
  123.         self.sock.listen(5)
  124.         _t1 = time.time()
  125.         self.failUnlessRaises(socket.error, self.sock.accept)
  126.         _t2 = time.time()
  127.         _delta = abs(_t1 - _t2)
  128.         self.assert_(_delta < _timeout + self.fuzz, 'timeout (%g) is %g seconds more than expected (%g)' % (_delta, self.fuzz, _timeout))
  129.  
  130.     
  131.     def testRecvfromTimeout(self):
  132.         _timeout = 2
  133.         self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  134.         self.sock.settimeout(_timeout)
  135.         self.sock.bind(self.addr_local)
  136.         _t1 = time.time()
  137.         self.failUnlessRaises(socket.error, self.sock.recvfrom, 8192)
  138.         _t2 = time.time()
  139.         _delta = abs(_t1 - _t2)
  140.         self.assert_(_delta < _timeout + self.fuzz, 'timeout (%g) is %g seconds more than expected (%g)' % (_delta, self.fuzz, _timeout))
  141.  
  142.     
  143.     def testSend(self):
  144.         pass
  145.  
  146.     
  147.     def testSendto(self):
  148.         pass
  149.  
  150.     
  151.     def testSendall(self):
  152.         pass
  153.  
  154.  
  155.  
  156. def test_main():
  157.     test_support.requires('network')
  158.     test_support.run_unittest(CreationTestCase, TimeoutTestCase)
  159.  
  160. if __name__ == '__main__':
  161.     test_main()
  162.  
  163.